import strutils
import strformat
import std/httpclient
import std/json

proc main(): void = 
  let Client = newHttpClient()
  try:
    let
      jsonResponse = parseJson(Client.getContent("http://localhost:5698/api/v2/transfer/info"))
      dl_data: int = parseInt($jsonResponse["dl_info_data"])
      dl_speed: int = parseInt($jsonResponse["dl_info_speed"])
      up_data: int = parseInt($jsonResponse["up_info_data"])
      up_speed: int = parseInt($jsonResponse["up_info_speed"])

    if (dl_data > 0 or dl_speed >= 1024*100):
      stdout.write(" ")
    if (dl_data > 0):
      stdout.write(fmt"{formatFloat(dl_data/(1024*1024*1024), ffDecimal, 1)}GB ")
    if (dl_speed >= 1024*100):
      stdout.write(fmt"{formatFloat(dl_speed/(1024*1024), ffDecimal, 1)}MB/s ")
      if (up_data >= 1024*1024 or up_speed >= 1024*10):
        stdout.write(" | ")

    if (up_data >= 1024*10240 or up_speed >= 1024):
      stdout.write(" ")
    if (up_data >= 1024*1024):
      stdout.write(fmt"{formatFloat(up_data/(1024*1024), ffDecimal, 1)}MB ")
    if (up_speed >= 1024):
      stdout.write(fmt"{int(up_speed/(1024))}KB/s")

  except OSError:
    return
  finally:
    discard ""
main()
